package com.qys.livesMall.shop.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.mapper.ShopGoodsInfoMapper;
import com.qys.livesMall.shop.modal.dto.ShopGoodsDto;
import com.qys.livesMall.shop.modal.dto.ShopProductAnchorOrderInfoDto;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.ShopGoodsInfo;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import com.qys.livesMall.shop.modal.query.SeachShopGoods;
import com.qys.livesMall.shop.modal.vo.ShopOrderSkuVO;
import com.qys.livesMall.shop.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2023-06-07
 */
@RestController
@RequestMapping("/sys/shopGoodsInfo")
@Tag(name = "后端-商铺商品信息", description = "后端-商品信息")
public class SysShopGoodsInfoController {
    @Resource
    ShopGoodsInfoService goodsInfoService;
    @Resource
    ShopGoodsSkuService skuService;

    @Resource
    ShopGoodsSkuStocksService stocksService;
    @Resource
    H2ShopBaseService baseService;
    @Resource
    private H2ShopBaseDao baseDao;
    @Resource
    ShopOrderSkuService orderSkuService;
    @Resource
    ShopGoodsInfoMapper goodsInfoMapper;
    @PostMapping("/page")
    @Operation(summary = "商品信息分页-商铺仓库")
    public R<Page<ShopGoodsDto>> page( @RequestBody SeachShopGoods data){
        System.out.println(data.getStatus());
        Page<ShopGoodsDto> page =Page.of(data.getCurrent(), data.getSize());
        page=goodsInfoMapper.queryShopGoods(page,data);
        if (ObjectUtils.isNotEmpty(page.getRecords())){
            for (ShopGoodsDto record : page.getRecords()) {
                LambdaQueryWrapper<ShopOrderSku> orderSkuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                orderSkuLambdaQueryWrapper.eq(ShopOrderSku::getStocksId,record.getStocksId());
                List<ShopOrderSku> orderSku = orderSkuService.list(orderSkuLambdaQueryWrapper.ne(ShopOrderSku::getAnchorId,0L));
                int anchorNum = orderSku.stream().mapToInt(ShopOrderSku::getNum).sum();
                orderSku=orderSkuService.list(orderSkuLambdaQueryWrapper.eq(ShopOrderSku::getVirtualUser,"0"));
                int navyNum = orderSku.stream().mapToInt(ShopOrderSku::getNum).sum();
                BigDecimal money = record.getPrice().multiply(new BigDecimal(record.getNum()));
                record.setAnchorNum(anchorNum);
                record.setNavyNum(navyNum);
                record.setTotalPrice(money);
            }
        }
        return R.ok(page);
//        LambdaQueryWrapper<ShopGoodsInfo> queryWrapper = new LambdaQueryWrapper<>();
//
//        if(ObjectUtils.isNotEmpty(data.getShopId())){
//            queryWrapper.eq(ShopGoodsInfo::getShopId,data.getShopId());
//        }
//
//        if (ObjectUtils.isNotEmpty(data.getName())){
//            queryWrapper.like(ShopGoodsInfo::getGoodsName,data.getName());
//        }
//        if (ObjectUtils.isNotEmpty(data.getCateId())){
//            queryWrapper.like(ShopGoodsInfo::getCategoryTwoId,data.getCateId());
//        }
//        queryWrapper.eq(ShopGoodsInfo::getIsDelete,"1");
//        Page<ShopGoodsInfo> page = Page.of(data.getCurrent(), data.getSize());
//        Page<ShopGoodsInfo> result = goodsInfoService.page(page, queryWrapper);
//        List<ShopGoodsInfo> shopGoodsInfoList = result.getRecords();
//        List<ShopGoodsInfoDto> shopGoodsInfoDtoList = new ArrayList<>();
//        for (ShopGoodsInfo shopGoodsInfo : shopGoodsInfoList) {
//
//            Long shopId = shopGoodsInfo.getShopId();
//            //查询用户
//            H2ShopBase shopBase = baseService.queryById(shopId);
//            //查询sku
//            LambdaQueryWrapper<ShopGoodsSku> skuLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            skuLambdaQueryWrapper.eq(ShopGoodsSku::getShopGoodsId,shopGoodsInfo.getId());
//            List<ShopGoodsSku> skus = skuService.list(skuLambdaQueryWrapper);
//            for (ShopGoodsSku sku : skus) {
//
//                //查询分批库存
//                LambdaQueryWrapper<ShopGoodsSkuStocks> stocksLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                stocksLambdaQueryWrapper.eq(ShopGoodsSkuStocks::getSkuId,sku.getId());
//                stocksLambdaQueryWrapper.eq(ShopGoodsSkuStocks::getState,false);
//                stocksLambdaQueryWrapper.ne(ShopGoodsSkuStocks::getNum, 0);
//                List<ShopGoodsSkuStocks> stocks = stocksService.list(stocksLambdaQueryWrapper);
//                for (ShopGoodsSkuStocks stock : stocks) {
//                    LambdaQueryWrapper<ShopOrderSku> orderSkuLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                    orderSkuLambdaQueryWrapper.eq(ShopOrderSku::getStocksId,stock.getId());
//                    orderSkuLambdaQueryWrapper.eq(ShopOrderSku::getShopId,sku.getShopId());
//                    List<ShopOrderSku> orderSku = orderSkuService.list(orderSkuLambdaQueryWrapper.ne(ShopOrderSku::getAnchorId,0L));
//                    int anchorNum = orderSku.stream().mapToInt(ShopOrderSku::getNum).sum();
//                    orderSku=orderSkuService.list(orderSkuLambdaQueryWrapper.eq(ShopOrderSku::getVirtualUser,"0"));
//                    int navyNum = orderSku.stream().mapToInt(ShopOrderSku::getNum).sum();
//                    BigDecimal money = stock.getPrice().multiply(new BigDecimal(stock.getNum()));
//                    ShopGoodsInfoDto infoDto = new ShopGoodsInfoDto();
//                    infoDto.setId(shopGoodsInfo.getId());
//                    infoDto.setState(shopGoodsInfo.isStatus());
//                    infoDto.setShopName(shopBase.getShopName());
//                    infoDto.setUserName(shopBase.getName());
//                    infoDto.setGoodsName(shopGoodsInfo.getGoodsName());
//                    infoDto.setSkuName(sku.getSkuName());
//                    infoDto.setPrice(stock.getPrice());
//                    infoDto.setCostPrice(stock.getCostPrice());
//                    infoDto.setNum(stock.getNum());
//                    infoDto.setAnchorNum(String.valueOf(anchorNum));
//                    infoDto.setNavyNum(String.valueOf(navyNum));
//                    infoDto.setPayType(stock.getPayType());
//                    infoDto.setTotalPrice(money);
//                    infoDto.setCreateTime(stock.getCreateTime());
//                    shopGoodsInfoDtoList.add(infoDto);
//                }
//            }
//
//        }
//        Page<ShopGoodsInfoDto> respage = new Page();
//        BeanUtil.copyProperties(page,respage);
//        respage.setRecords(shopGoodsInfoDtoList);
//        return R.ok(respage);
    }

    @Operation(summary = "商品上下架")
    @GetMapping("/{goodsId}")
    public R Controls(@PathVariable Long goodsId){
        try {
            ShopGoodsInfo goodsInfo = goodsInfoService.getById(goodsId);
            goodsInfo.setUpShelfDate(LocalDateTime.now());
            goodsInfo.setStatus(!goodsInfo.isStatus());
            goodsInfoService.updateById(goodsInfo);
            LambdaQueryWrapper<ShopGoodsInfo> shopGoodsInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::getShopId,goodsInfo.getShopId());
            shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::getShopId,goodsInfo.getShopId());
            shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::isStatus,true);
            shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::getIsDelete,"1");
            Long shopProductNum = goodsInfoService.count(shopGoodsInfoLambdaQueryWrapper);
            H2ShopBase shopBase = baseDao.getById(goodsInfo.getShopId());
            shopBase.setShopProductNum(Integer.valueOf(shopProductNum.toString()));
            return baseDao.updateById(shopBase)?R.ok():R.fail();
        }catch (QysException e){
            return R.fail(e.getMessage());
        }
    }

    @PostMapping("/list")
    @Operation(summary = "店铺所有商品列表信息-商铺仓库")
    public R<List<ShopGoodsInfo>> list( @RequestBody SeachShopGoods data){

        if(data.getShopId()==null){
            return R.ok();
        }
        LambdaQueryWrapper<ShopGoodsInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoodsInfo::getShopId,data.getShopId());
        queryWrapper.eq(ShopGoodsInfo::getIsDelete,"1");
        if (ObjectUtils.isNotEmpty(data.getName())){
            queryWrapper.like(ShopGoodsInfo::getGoodsName,data.getName());
        }
       List<ShopGoodsInfo> list= goodsInfoService.list(queryWrapper);
        return R.ok(list);
    }

    @Operation(summary = "主播带货")
    @PostMapping(value = "/productAnchorOrder/list")
    public R<?> listProductAnchorOrder(@RequestBody ShopProductAnchorOrderInfoDto shopProductAnchorDto){
        Page<ShopOrderSkuVO> shopOrderSkuVOPage = orderSkuService.listProductAnchorOrder(shopProductAnchorDto);
        return R.ok(shopOrderSkuVOPage);
    }

    @Operation(summary = "带货商品详情")
    @GetMapping(value = "/productAnchor/{skuId}")
    public R<?> getProductAnchorInfo(@PathVariable String skuId){
        return R.ok(orderSkuService.getProductAnchorInfo(skuId));
    }
}
